package com.sam;


import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.PixelFormat;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.VideoView;

public class VideoActivity extends Activity {

	 private static ProgressDialog progressDialog;
     String videourl="http://192.168.184.1:8000/video/";
     VideoView videoView ;
     Button btnF;
     Button btnR;
     SocketService mBoundService;
     boolean mIsBound = false;
     String a =null;
     
     private ServiceConnection mConnection = new ServiceConnection() {
 		// EDITED PART
 		@SuppressWarnings("deprecation")
 		@Override
 		public void onServiceConnected(ComponentName name, IBinder service) {
 			// TODO Auto-generated method stub
 			mBoundService = ((SocketService.LocalBinder) service).getService();
 			
 			btnF.setEnabled(true);
			btnR.setEnabled(true);
			
			btnR.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					String vid[] =new String[1];
					vid[0] = a;
					 mBoundService.getTCPClient().sendMessage(new DataStream("real",null,vid));
					 btnF.setEnabled(false);
					 btnR.setEnabled(false);     

					
				}
			});
			
			btnF.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					String vid[] =new String[1];
					vid[0] = a;
					mBoundService.getTCPClient().sendMessage(new DataStream("false",null,vid));
					 btnF.setEnabled(false);
					 btnR.setEnabled(false); 
					
				}
			});
 			
 			
 			

 		}

 		@Override
 		public void onServiceDisconnected(ComponentName name) {
 			// TODO Auto-generated method stub
 			mBoundService = null;
 		}

 	};
 	
 	private void doBindService() {
 		bindService(new Intent(this, SocketService.class), mConnection,
 				Context.BIND_AUTO_CREATE);
 		mIsBound = true;
 		if (mBoundService != null) {
 			mBoundService.IsBoundable();
 		}
 	}

 	private void doUnbindService() {
 		if (mIsBound) {
 			// Detach our existing connection.
 			unbindService(mConnection);
 			mIsBound = false;
 		}
 	}

     

     @Override
     protected void onCreate(Bundle savedInstanceState)
     {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.video_main);
            
            Bundle extras = getIntent().getExtras();
    		a = extras.getString("name");
    		
    		btnR = (Button) findViewById(R.id.real);
    		
    		btnF =  (Button) findViewById(R.id.notreal);
    		
    		btnR.setEnabled(false);
    		btnF.setEnabled(false);
    		
            
    		doBindService();
            videourl=videourl+a+".3gp";
            
            Log.i("video path",videourl);
            videoView = (VideoView) findViewById(R.id.video_View);

            progressDialog = ProgressDialog.show(VideoActivity.this, "", "Buffering video...",true);
            progressDialog.setCancelable(false);

  
            PlayVideo();
     }

     private void PlayVideo()
     {
            try
            {   
                   getWindow().setFormat(PixelFormat.TRANSLUCENT);
                   MediaController mediaController = new MediaController(VideoActivity.this);
                   mediaController.setAnchorView(videoView);        

                   Uri video = Uri.parse(videourl);          
                   videoView.setMediaController(mediaController);
                   videoView.setVideoURI(video);
                   videoView.requestFocus();           
                   videoView.setOnPreparedListener(new OnPreparedListener()
                   {

                         public void onPrepared(MediaPlayer mp)
                         {                
                                progressDialog.dismiss();  
                                videoView.start();
                         }
                   });

            }
            catch(Exception e)
            {
                   progressDialog.dismiss();
                   System.out.println("Video Play Error :"+e.toString());
                   finish();
            }

     }
     @Override
 	protected void onDestroy() {
 		super.onDestroy();
 		doUnbindService();
 	}

}
